package com.wdhcr.controller.admin;

import com.wdhcr.entity.Blog;
import com.wdhcr.enums.ResultCode;
import com.wdhcr.service.BlogService;
import com.wdhcr.utils.ResponseResult;
import com.wdhcr.utils.WxUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/*
 * @description: 处理博客管理的接口
 * @author: wdhcr
 * @dateTime: 2020/3/23 17:05
 */
@RestController
public class BlogController {


    @Autowired
    private BlogService blogService;

    private WxUtil wxUtil;


    /*
     * @description: 添加博客的接口方法，使用@RequestBody要注意前端
     * 的数据格式一定要和entity中的相同，否则会保存失败
     * @params: [blog]
     * @return: com.wdhcr.utils.ResponseResult
     * @author: wdhcr
     * @dateTime: 2020/3/21 18:53
     */
    @CrossOrigin //该注解声明这个接口支持跨域请求
    @PostMapping("/saveBlog")
    public ResponseResult saveBlog(@RequestBody Blog blog) throws Exception {
        try {


        Blog blog1 = blogService.saveBlog(blog);
        if (blog1 != null){
            return new ResponseResult(ResultCode.SUCCESS.getCode(),"保存成功！",null);
        }}catch (Exception e){
            throw new Exception("保存失败!");
        }
        return new ResponseResult(ResultCode.FAIL.getCode(),"保存失败！",null);
    }

   /*
    * @description: 前端分页查询的条数的接口
    * @params: [page, size]
    * @return: com.wdhcr.utils.ResponseResult
    * @author: wdhcr
    * @dateTime: 2020/3/21 21:14
    */
   @CrossOrigin
    @GetMapping("getPageList/{page}/{size}")
    public ResponseResult getPageBlogList(@PathVariable("page") Integer page,@PathVariable("size") Integer size){
       //根据更新时间倒叙排列
       Sort update_time = Sort.by(Sort.Direction.DESC,"updateTime");
       PageRequest pageRequest = PageRequest.of(page, size,update_time);
        Page<Blog> all = blogService.findAll(pageRequest);
        if (!all.isEmpty()){
            return new ResponseResult(ResultCode.SUCCESS.getCode(),"获取列表成功！",all);
        }
        return new ResponseResult(ResultCode.SUCCESS.getCode(),"暂无博客信息！",all);

    }
    /*
     * @description: 根据id查询对应博客信息
     * @params: [id]
     * @return: com.wdhcr.utils.ResponseResult
     * @author: wdhcr
     * @dateTime: 2020/3/21 22:21
     */
    @CrossOrigin
    @GetMapping("/getBlogById/{id}")
    public ResponseResult getBlogById(@PathVariable("id") Integer id){
        Blog byId = blogService.findById(id);
        if (byId != null){
            return new ResponseResult(ResultCode.SUCCESS.getCode(),"获取成功！", byId);
        }
        return new ResponseResult(ResultCode.SUCCESS.getCode(),"找不到了，看看其他的吧！",null);
    }

    /*
     * @description: 提供搜索功能的接口
     * @params: [Keyword] 搜索关键字
     * @return: com.wdhcr.utils.ResponseResult
     * @author: wdhcr
     * @dateTime: 2020/3/23 16:13
     */
    @CrossOrigin
    @PostMapping("searchBlog")
    public ResponseResult searchBlog(@RequestBody String query){
        String dquery = null;
        try {
            dquery = URLDecoder.decode(query, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //根据更新时间倒叙排列
        Sort update_time = Sort.by(Sort.Direction.DESC,"updateTime");
        PageRequest pageRequest = PageRequest.of(0, 10000,update_time);

        Page<Blog> all = blogService.findByQuery(pageRequest,dquery.replace("=",""));
        if (!all.isEmpty()){
            return new ResponseResult(ResultCode.SUCCESS.getCode(),"获取列表成功！",all);
        }
        return new ResponseResult(ResultCode.SUCCESS.getCode(),"暂时找不到，试试其他的吧！",all);
    }
    @CrossOrigin
    @PostMapping("delete/{id}")
    public ResponseResult delete(@PathVariable Integer id){
        return blogService.deleteById(id);
    }
}
